package com.rabbiter.oes.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.oes.dto.StudentExamDTO;
import com.rabbiter.oes.entity.ExamManage;
import com.rabbiter.oes.entity.Student;
import com.rabbiter.oes.mapper.ExamManageMapper;
import com.rabbiter.oes.service.ExamManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ExamManageServiceImpl extends ServiceImpl<ExamManageMapper, ExamManage> implements ExamManageService {

    @Autowired
    private PaperServiceImpl paperService;

    private void setMaxScore(List<ExamManage> examManageList) {
        for (ExamManage examManage : examManageList) {
            examManage.setTotalScore(paperService.getMaxScore(examManage.getPaperId()));
        }
    }

    @Override
    public List<ExamManage> findAll() {
        List<ExamManage> examManages = baseMapper.findAll();
        return examManages;
    }

    @Override
    public IPage<ExamManage> findPage(Page<ExamManage> page) {
        Page<ExamManage> iPage = baseMapper.findPage(page);
        return iPage;
    }

    @Override
    public IPage<ExamManage> findPage(Page<ExamManage> page,String description, Integer collegeId, Integer majorId) {
        description = ("@".equals(description) ? "" : description);
        IPage<ExamManage> iPage = baseMapper.findPageBytj(page, description, collegeId, majorId);
        return iPage;
    }

    @Override
    public IPage<ExamManage> studentFindPage(StudentExamDTO dto, Student student) {
        Page<ExamManage> page = new Page<>(dto.getPage(), dto.getSize());
        // 学生专业id
        Integer majorId = student.getMajorId();
        // 学生学院id
        Integer collegeId = student.getCollegeId();
        return baseMapper.studentFindPage(page, dto, majorId, collegeId);
    }

    @Override
    public ExamManage findById(Integer examCode) {
        ExamManage examManage = baseMapper.findById(examCode);
        examManage.setTotalScore(paperService.getMaxScore(examManage.getPaperId()));
        return examManage;
    }

    @Override
    @Transactional
    public int delete(Integer examCode) {
        // 移除题目关联
        ExamManage examManage = baseMapper.findById(examCode);
        Assert.isTrue(ObjectUtil.isNotEmpty(examManage), "未获取到要删除的试卷信息");
        paperService.deleteByPaperId(examManage.getPaperId());
        return baseMapper.deleteById(examCode);
    }

    @Override
    public int update(ExamManage exammanage) {
        return baseMapper.updateById(exammanage);
    }

    @Override
    public int add(ExamManage exammanage) {
        return baseMapper.insert(exammanage);
    }

    @Override
    public ExamManage findOnlyPaperId() {
        List<ExamManage> examManages = baseMapper.findOnlyPaperId();
        if (examManages.size() > 0) {
            return examManages.get(0);
        } else {
            ExamManage examManage = new ExamManage();
            examManage.setPaperId(1);
            return examManage;
        }
    }
}
